package com.itheima.web.servlet;

import com.itheima.domain.PageBean;
import com.itheima.domain.Route;
import com.itheima.factory.BeanFactory;
import com.itheima.service.RouteService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/RouteServlet")
public class RouteServlet  extends BaseServlet{
      RouteService routeService= (RouteService) BeanFactory.getBean("routeService");


    //模板
    protected void xxx(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    // 线路详情
    protected void routeDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.接收请求参数 rid
        String rid = request.getParameter("rid");
        // 2.调用sercie查询
        Route route = routeService.findDetail(rid);
        // 3.设置到reuqest域
        request.setAttribute("route", route);
        // 4.转发到 route_detail.jsp
        request.getRequestDispatcher("/route_detail.jsp").forward(request, response);
    }
    //分页查询
    protected void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.接受请求参数
        String currentPagestr = request.getParameter("currentPage");
        String pageSizestr = request.getParameter("pageSize");
        String cid = request.getParameter("cid");
        String rname = request.getParameter("rname");
        //2.类型转换
        if (currentPagestr==null||currentPagestr.equals("")){
            currentPagestr="1";
        }
        int currentPage = Integer.parseInt(currentPagestr);
        if (pageSizestr==null||pageSizestr.equals("")){
            pageSizestr="10";
        }
        int pageSize = Integer.parseInt(pageSizestr);
       //3.调用service分页查询
        PageBean<Route>pb=routeService.findByPage(currentPage,pageSize,cid,rname);
        //4.将分页对象设置到request域中
        request.setAttribute("pb",pb);
         request.setAttribute("cid",cid);//回显分类id
        request.setAttribute("rname",rname);//回显搜索关键字
         //5.转发到 route_list.jsp
        request.getRequestDispatcher("/route_list.jsp").forward(request,response);
    }

}
